package cn.wolfcode.web.controller;


import cn.wolfcode.domain.SystemDictionary;
import cn.wolfcode.domain.SystemDictionaryItem;
import cn.wolfcode.qo.SystemDictionaryItemQueryObject;
import cn.wolfcode.service.ISystemDictionaryItemService;
import cn.wolfcode.service.ISystemDictionaryService;
import cn.wolfcode.util.RequiredPermission;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/systemDictionaryItem")
public class SystemDictionaryItemController {

    @Autowired
    private ISystemDictionaryItemService systemDictionaryItemService;

    @Autowired
    private ISystemDictionaryService systemDictionaryService;


    @RequiredPermission(name="数据字典明细页面",expression="systemDictionaryItem:list")
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") SystemDictionaryItemQueryObject qo){
        PageInfo<SystemDictionaryItem> pageInfo = systemDictionaryItemService.query(qo);
        List<SystemDictionary> systemDictionaries = systemDictionaryService.listAll();
        model.addAttribute("sds",systemDictionaries);
        model.addAttribute("pageInfo", pageInfo);
        return "systemDictionaryItem/list";
    }


    @RequiredPermission(name="数据字典明细删除",expression="systemDictionaryItem:delete")
    @RequestMapping("/delete")
    public String delete(Long id) {
        if (id != null) {
           systemDictionaryItemService.delete(id);
        }
        return "redirect:/systemDictionaryItem/list";
    }


    @RequiredPermission(name="数据字典明细新增",expression="systemDictionaryItem:save")
    @RequestMapping("/save")
    public String save(SystemDictionaryItem systemDictionaryItem) {
            systemDictionaryItemService.save(systemDictionaryItem);
            return "redirect:/systemDictionaryItem/list";
    }

    @RequiredPermission(name="数据字典明细编辑",expression="systemDictionaryItem:update")
    @RequestMapping("/update")
    public String update(SystemDictionaryItem systemDictionaryItem) {
            systemDictionaryItemService.update(systemDictionaryItem);
            return "redirect:/systemDictionaryItem/list";
    }


    @RequiredPermission(name="数据字典明细新增/编辑",expression="systemDictionaryItem:saveOrUpdate")
    @RequestMapping("/input")
    public String input(Long id, Model model) {
        if (id != null) {
            model.addAttribute("systemDictionaryItem",systemDictionaryItemService.get(id));
        }
        return "systemDictionaryItem/input";
    }

    @RequestMapping("/saveInput")
    @ResponseBody
    public List<SystemDictionaryItem> saveInput(Long typeId){
        return systemDictionaryItemService.listAll(typeId);
    }
}
